<?php
namespace app\api\controller;


use app\api\model\Createclass; 
use \think\Validate;


class Classwork extends Baseic
{

    public function __auto()
    {
        $this->model = new Createclass;
    }

    /**
    * 上传课前文档
    * 参数 pic文件 name文件名 typeid所属分类
    * 类型 文件
    */
    public function uploadClass()
    {   
        if ($this->request->isPost()) {

            $pic = input('pic'); // 文件
            $img = $this->uplode($pic);
            $data['url'] = json_encode($img);
            $data['typeid'] = input('typeid'); // 所属分类
            $data['name'] = input('name'); // 文件名

            $validate = new Validate([
                'name'   => 'require',
                'typeid' => 'require',
                'url'    => 'require',
            ]);
            $data = [
                'name'   => $data['name'],
                'typeid' => $data['typeid'],
                'url'    => $data['url'],
            ];

            if (!$validate->check($data)) {
                $this->Retterror(config('Notice.AllError'));
                return $this->retResponse();
            }
            
            $data['time'] = time();

            $this->data = $this->model->uploaddoc($data);

            if($this->data){
                $this->Rettsuccess();
                return $this->retResponse();
            }else{
                $this->Retterror(config('Notice.AllError'));
                return $this->retResponse();
            }

        }

    }

    /**
    * 上传课前文档绑定课前文档到预约教师
    * 参数 class_id房间id  file_id文件id
    */
    public function workToclass()
    {
        if ($this->request->isPost()) {
            $data['id'] = input('class_key');//房间id
            $data['tid'] = input('file_id');//文件id
            
            $this->data = $this->model->uploadClass($data);

            if($this->data){
                $this->Rettsuccess();
                return $this->retResponse();
            }else{
                $this->Retterror(config('Notice.AllError'));
                return $this->retResponse();
            }
        }
    }


    /**
    * 解除课前文档与预约教室的绑定
    * 参数 class_id房间id  file_id文件id
    */
    public function deleTowork()
    {
        if ($this->request->isPost()) {
            $data['id'] = input('class_key');//房间id
            $data['tid'] = input('file_id');//文件id
            
            $this->data = $this->model->deleteClass($data);

            if($this->data){
                $this->Rettsuccess();
                return $this->retResponse();
            }else{
                $this->Retterror(config('Notice.AllError'));
                return $this->retResponse();
            }
        }
    }



    /** 根据房间号获取课件
     *  PC端接口
     */

    public function HaveWork()
    {
        $id = input('id');//房间id
        $this->data = $this->model->Index($id);
        if($this->data){
            $this->Rettsuccess();
            return $this->retResponse();
        }else{
            $this->Retterror();
            return $this->retResponse();
        }
    }
}